package com.example.mybatisplusdemo.controller;

import cn.hutool.core.bean.BeanUtil;
import com.example.mybatisplusdemo.domain.dto.UserDTO;
import com.example.mybatisplusdemo.domain.po.User;
import com.example.mybatisplusdemo.domain.vo.UserVo;
import com.example.mybatisplusdemo.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags="用户管理接口")
@RestController
@RequestMapping("/api/users")
public class TestController {

    @Autowired
    private IUserService userService;

    @ApiOperation("新增用户接口")
    @PostMapping
    public void saveUser(@RequestBody UserDTO userDTO){
        User user = BeanUtil.copyProperties(userDTO, User.class);
        userService.save(user);
    }

    @ApiOperation("删除用户接口")
    @DeleteMapping("{id}")
    public void deleteUser (@ApiParam("用户id") @PathVariable("id") Long id){
        userService.removeById(id);
    }

    @ApiOperation("查询用户接口")
    @GetMapping("{id}")
    public UserVo queryUserById (@ApiParam("用户id") @PathVariable("id") Long id){
        User user = userService.getById(id);
        // 把user拷贝到userVo
        UserVo userVo = BeanUtil.copyProperties(user, UserVo.class);
        return userVo;
    }

    @ApiOperation("查询id批量查询用户接口")
    @GetMapping
    public List<UserVo> queryUserById (@ApiParam("用户id集合") @RequestParam("ids") List<Long> ids){
        List<User> users = userService.listByIds(ids);
        // 把user拷贝到userVo
        List<UserVo> userVos = BeanUtil.copyToList(users, UserVo.class);
        return userVos;
    }
}
